from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager, AbstractUser
from django.db import models

from shortuuidfield import ShortUUIDField


class UserManager(BaseUserManager):
    def _create_user(self, account, password, **kwargs):
        instance = self.model(account=account, **kwargs)
        instance.set_password(password)
        instance.save()
        return instance

    def create_user(self, account, password, **kwargs):
        kwargs['is_staff'] = False
        kwargs['is_superuser'] = False
        return self._create_user(account=account, password=password, **kwargs)

    def create_superuser(self, account, password, **kwargs):
        kwargs['is_staff'] = True
        kwargs['is_superuser'] = True
        return self._create_user(account=account, password=password, **kwargs)


class UserInfo(AbstractBaseUser, PermissionsMixin):
    GENDER_TYPE = (
        (0, "保密"),
        (1, "男"),
        (2, "女"),
    )

    uid = ShortUUIDField(primary_key=True)
    account = models.CharField(verbose_name="账号",
                               max_length=20,
                               unique=True)
    nickname = models.CharField(verbose_name="昵称",
                                max_length=20,
                                null=True,
                                blank=True)
    age = models.IntegerField(verbose_name="年龄",
                              null=True,
                              blank=True)
    gender = models.IntegerField(verbose_name="性别",
                                 choices=GENDER_TYPE,
                                 default=0)
    sign = models.CharField(verbose_name="签名",
                            max_length=300)
    up = models.IntegerField(verbose_name="获赞",
                             default=0)
    phone = models.CharField(verbose_name="手机号码",
                             max_length=11,
                             null=True,
                             blank=True)
    avatar = models.CharField(verbose_name="用户头像",
                              max_length=100,
                              null=True,
                              blank=True)
    is_active = models.BooleanField(verbose_name="激活状态",
                                    default=True)
    date_join = models.DateTimeField(verbose_name="加入时间",
                                     auto_now_add=True)

    objects = UserManager()
    USERNAME_FIELD = 'account'
    REQUIRED_FIELDS = []

    def __str__(self):
        return self.nickname or self.account

    def get_full_name(self):
        return self.nickname or self.account

    def get_short_name(self):
        return self.nickname or self.account

    # def email_user(self, subject, message, from_email=None, **kwargs):
    #     """Send an email to this user."""
    #     send_mail(subject, message, from_email, [self.email], **kwargs)

    class Meta:
        db_table = 'el_user'
        verbose_name = "用户"
        verbose_name_plural = verbose_name
        default_permissions = ()
        permissions = (
            ("view_userinfo", "查看用户"),
            ("create_userinfo", "创建用户"),
            ("update_userinfo", "更新用户"),
            ("delete_userinfo", "删除用户"),
        )


class Follow(models.Model):
    uid = models.ForeignKey(UserInfo,
                            on_delete=models.CASCADE,
                            related_name="follows")
    fid = models.ForeignKey(UserInfo,
                            on_delete=models.CASCADE)
    date_join = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'el_follow'
        verbose_name = "粉丝"
        verbose_name_plural = verbose_name
        default_permissions = ()
